﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;



namespace ScriptEase
{
    internal static class FileNDirectory
    {
        public static string AlignFileName(string fileName)
        {
            if (string.IsNullOrWhiteSpace(fileName)) return String.Empty;

            string correctFileName = alignPath(fileName, Path.GetInvalidFileNameChars(), '_');
            if (correctFileName != fileName)
            {
                Trace.Default.Warn("Correcting Filename {0} to {1}!", fileName, correctFileName);
            }
            return correctFileName;
        }



        public static string AlignDirectoryName(string directoryName)
        {
            if (string.IsNullOrWhiteSpace(directoryName)) return String.Empty;

            string correctDirectoryName = alignPath(directoryName, Path.GetInvalidPathChars(), '_');
            if (correctDirectoryName != directoryName)
            {
                Trace.Default.Warn("Correcting Directoryname {0} to {1}!", directoryName, correctDirectoryName);
            }
            return correctDirectoryName;
        }



        private static string alignPath(string path, IEnumerable<char> invalidCharacters, char replacement)
        {
            List<char> invalidChars = invalidCharacters.ToList();
            invalidChars.Add('(');
            invalidChars.Add(')');

            StringBuilder sb = new StringBuilder(path);
            sb = invalidChars.Aggregate(sb, (current, invalidChar) => current.Replace(invalidChar, replacement));
            return sb.ToString();
        }
    }
}